import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
from gi.repository import Gst, GstRtspServer, GObject

class MyFactory(GstRtspServer.RTSPMediaFactory):
    def __init__(self):
        super(MyFactory, self).__init__()

    def do_create_element(self, url):
        pipeline_str = (
            "videotestsrc ! x264enc ! rtph264pay name=pay0 pt=96"
        )
        return Gst.parse_launch(pipeline_str)

def main():
    GObject.threads_init()
    Gst.init(None)

    server = GstRtspServer.RTSPServer()
    factory = MyFactory()
    factory.set_shared(True)

    mount_points = server.get_mount_points()
    mount_points.add_factory("/test", factory)

    server.attach(None)
    print("RTSP server is running at rtsp://127.0.0.1:8554/test")
    loop = GObject.MainLoop()
    loop.run()

if __name__ == "__main__":
    main()
